

import pandas as pd
from django.shortcuts import render
from django.http.response import HttpResponseBadRequest

from backend import schedule_jobs
from mysite import db_config
from utils.functions import is_login
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore,register_job
import logging

import utils.http_response as hr
logger = logging.getLogger('log')
scheduler = BackgroundScheduler()  # 创建一个调度器对象
store = DjangoJobStore()
scheduler.add_jobstore(store, "default")  # 添加一个作业
scheduler.start()

@is_login
def database(request):
    """列出数据源
    """
    return render(request, "backend/database.html")


@is_login
def database_detail(request):
    """查看数据源详情
    """
    id = request.GET.get('id')
    
    try:
        conn = db_config.sqlalchemy_conn()
        db = pd.read_sql(f"select name,ip,user,db,port,db_type,charset,note from source_db_info where id={id} ", con=conn)
        return render(request, "backend/database_detail.html", {
                                                            'name': db['name'].values.tolist()[0],
                                                            'ip': db['ip'].values.tolist()[0],
                                                            'user': db['user'].values.tolist()[0],
                                                            'db': db['db'].values.tolist()[0],
                                                            'port': db['port'].values.tolist()[0],
                                                            'db_type': db['db_type'].values.tolist()[0],
                                                            'charset': db['charset'].values.tolist()[0],
                                                            'note': db['note'].values.tolist()[0],
                                                            'id': id
                                                        })
    except Exception as e:
        return HttpResponseBadRequest(content=e)
    finally:
        conn.dispose()


@is_login
def database_add(request):
    """新增数据源
    """
    return render(request, "backend/database_add.html")


@is_login
def crontab(request):
    """列出后台管理的定时任务
    """
    data = []
    # cron = CronTab(user=True)
    # job = list(cron.find_comment(re.compile(r'backend')))
    # if job:
    #     for i in job:
    #         enable = i.is_enabled()
    #         job_time = i.description(use_24hour_time_format=True, locale_code='zh_CN') # 获取crontab的度周期
    #         comment = i.comment
    #         command = i.command
    #         data.append([comment, command, enable, job_time])
    #         return render(request, "backend/crontab.html", {"jobs": data})
    # else:
    #     return render(request, "backend/crontab.html", {"jobs": None})
    data = schedule_jobs.get_jobs()

    return render(request, "backend/crontab.html", {"jobs":data})


@is_login
def menu(request):

    return render(request, "backend/menu.html", {"data": None})

@is_login
def menu_add(request):
    return render(request, "backend/menu_add.html", {"data": None})





